package de.fhluebeck.oop.gps.gui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import de.fhluebeck.oop.gps.common.Constants;
import de.fhluebeck.oop.gps.common.DependencyManager;
import de.fhluebeck.oop.gps.entity.Category;
import de.fhluebeck.oop.gps.service.ICategoryService;

/**
 * 
 * TableModel zur Darstellung von Daten vom Typ Category in einer JTable
 * 
 * @author Ogün Bilge, Leif Hitzschke
 * @version 1.00, 13.11.2009
 *
 */
public class CategoryTableModel extends AbstractTableModel {
	
	protected List<Category> listOfCategory = new ArrayList<Category>();
	private ICategoryService categoryService = (ICategoryService) DependencyManager.locate(Constants.CATEGORY_SERVICE_DEP_NAME);
	private String[] header = new String[] {"Nr.", "Art", "Beschreibung"};
	
	public CategoryTableModel() {
		this.loadData();
	}
	
	protected void loadData() {
		listOfCategory = categoryService.readAll();
	}
	
	public int getCountOfHeaders() {
		return this.header.length;
	}
	
	public String getColumnName(final int spalte) {
		if(spalte < this.getCountOfHeaders()) {
			return header[spalte];
		}
		else {
			return super.getColumnName(spalte);
		}
	}
	
	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public int getRowCount() {
		return this.listOfCategory.size();
	}

	@Override
	public Object getValueAt(final int zeile, final int spalte) {
		switch(spalte) {
		case 0:
			return this.listOfCategory.get(zeile).getId();
		case 1:
			return this.listOfCategory.get(zeile).getName();
		case 2:
			return this.listOfCategory.get(zeile).getDescription();
		default:
			return null;
		}
	}

}
